import {Component, OnInit, ViewEncapsulation, ChangeDetectionStrategy} from '@angular/core';
import {Router, ActivatedRoute} from "@angular/router";
import {CrowdService} from "@crowd/crowd.service";

@Component({
  selector: 'dynamic-issue',
  templateUrl: './dynamic-issue.component.html',
  styleUrls: ['./dynamic-issue.component.scss'],
  encapsulation: ViewEncapsulation.None,
})
export class DynamicIssueComponent implements OnInit {
  public activeDynamic = '';
  public Dynamic: string;
  public crowdCode: string;
  public id: number;
  public data: any = {
    activeDynamic: '',
    crowdCode: ''
  };

  constructor(public router: Router,
              public route: ActivatedRoute,
              private crowdSer: CrowdService
  ) {
    this.route.queryParams.subscribe(params => {
      this.data.crowdCode = params['crowdCode'];
    });
  }

  // 发布活动动态
  setCrowdDynamics(data: any) {
    this.crowdSer.setCrowdDynamics(data).subscribe(res => {
      if (res.result === '0') {
        this.router.navigate([`/crowd/crowd-details`], {queryParams: {crowdCode: this.data.crowdCode}});
      };
    });
  }

  leftTrim(str: string) {
    return str.replace(/^\s*/, ""); // 从开头进行匹配，去除空格
  }

  /*
   (ngModelChange)="change()" // html页面中的
   change(){
   this.data.activeDynamic = this.leftTrim( this.data.activeDynamic);
   }
   */

  // 跳转众筹详情
  goToManage() {
    this.setCrowdDynamics(this.data);
  }

  ngOnInit() {

  }

}
